#include "bglreader.h"
#include "bglfile.h"
#include "dictbuilder.h"

#include <wx/wx.h>
#include <wx/filename.h>

BGLReader::BGLReader(wxString filename, DictBuilder *builder,
        const wxString &source_charset, const wxString &target_charset) {
    m_babylon = new BGLFile( filename, source_charset, target_charset);
    m_builder = builder;
}

bool BGLReader::convert() {
    if ( !m_babylon->open() ) {
        wxPrintf(_("Error openning %s\n"), m_babylon->file_name.c_str() );
        return false;
    }

    m_babylon->read_info();
    m_builder->title = m_babylon->title;
    m_builder->author = m_babylon->author;
    m_builder->email = m_babylon->email;
    m_builder->license = m_babylon->copyright;
    m_builder->origLang = m_babylon->sourceLang;
    m_builder->destLang = m_babylon->targetLang;
    m_builder->description = m_babylon->description;
    m_builder->defaultCharset = m_babylon->defaultCharset;
    m_builder->targetCharset = m_babylon->targetCharset;
    m_builder->sourceCharset = m_babylon->sourceCharset;
    m_builder->write_info();
    
    wxFileName::Mkdir(_T("res"),0777,wxPATH_MKDIR_FULL);
    
    bgl_entry entry;
    entry = m_babylon->readEntry();
    
    int n = 0;
    while (entry.headword != _T("")) {
        m_builder->addHeadword(entry.headword, entry.definition);
        entry = m_babylon->readEntry();
        n++;
        if (n%100== 1) {
            wxPrintf(_T("."));
            fflush(stdout);
        }
    }
    wxPrintf(_T("\n"));
    
    m_babylon->close();
    
    return true;
}
